/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package l1j.server.server.model;

import static l1j.server.server.model.skill.L1SkillId.AWAKEN_ANTHARAS;
import static l1j.server.server.model.skill.L1SkillId.AWAKEN_FAFURION;
import static l1j.server.server.model.skill.L1SkillId.AWAKEN_VALAKAS;
import static l1j.server.server.model.skill.L1SkillId.SHAPE_CHANGE;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_ChangeShape;
import l1j.server.server.serverpackets.S_CharVisualUpdate;
import l1j.server.server.serverpackets.S_HPUpdate;
import l1j.server.server.serverpackets.S_OwnCharAttrDef;
import l1j.server.server.serverpackets.S_OwnCharStatus;
import l1j.server.server.serverpackets.S_SPMR;


/**
 * ドラゴンナイト リニューアル関連、覚醒リニューアル
 * 覚醒の種類によって見た目を変更
 * ボーナス効果の調整
 * アンタラス：AC-8、HP+35
 * パプリオン：MR+15%、全ての属性抵抗+15
 * ヴァラカス：全てのパラメータ(STRなど)+3
 */
public class L1Awake {

	//TODO 3.52C龍騎士覺醒狀態外型調整變更
	public static void doPoly(final L1PcInstance pc, final int skillId) {
		int polyId = 0;
		if (skillId == AWAKEN_ANTHARAS) {//TODO 覺醒：安塔瑞斯
			polyId = 9362;
		} else if (skillId == AWAKEN_FAFURION) {//TODO 覺醒：法力昂
			polyId = 9364;
		} else if (skillId == AWAKEN_VALAKAS) {//TODO 覺醒：巴拉卡斯
			polyId = 9363;
		}
		//TODO 3.52C龍騎士覺醒狀態外型調整變更
		if (pc.hasSkillEffect(SHAPE_CHANGE)) {
			pc.killSkillEffectTimer(SHAPE_CHANGE);
		}
		pc.setTempCharGfx(polyId);
		pc.sendPackets(new S_ChangeShape(pc.getId(), polyId));
		if (!pc.isGmInvis() && !pc.isInvisble()) {
			pc.broadcastPacketAll(new S_ChangeShape(pc.getId(), polyId));
		}
		final L1ItemInstance weapon = pc.getWeapon();
		if (weapon != null) {
			final S_CharVisualUpdate charVisual = new S_CharVisualUpdate(pc);
			pc.sendPackets(charVisual);
			pc.broadcastPacketAll(charVisual);
		}
	}

	public static void start(final L1PcInstance pc, final int skillId) {
		if (skillId == pc.getAwakeSkillId()) {// 再詠唱なら解除
			stop(pc);
		} else if (pc.getAwakeSkillId() != 0) {// 他の覚醒スキルの場合はリターン
			return;
		} else {
			if (skillId == AWAKEN_ANTHARAS) {// 覚醒：アンタラス
				pc.addMaxHp(35);
				pc.sendPackets(new S_HPUpdate(pc.getCurrentHp(), pc.getMaxHp()));
				if (pc.isInParty()) {// パーティー中
					pc.getParty().updateMiniHP(pc);
				}
				pc.addAc(-8);
			} else if (skillId == AWAKEN_FAFURION) {// 覚醒：パプリオン
				pc.addMr(25);
				pc.sendPackets(new S_SPMR(pc));
				pc.addWind(15);
				pc.addWater(15);
				pc.addFire(15);
				pc.addEarth(15);
				pc.sendPackets(new S_OwnCharAttrDef(pc));
			} else if (skillId == AWAKEN_VALAKAS) {// 覚醒：ヴァラカス
				pc.addStr(3);
				pc.addCon(3);
				pc.addDex(3);
				pc.addCha(3);
				pc.addInt(3);
				pc.addWis(3);
			}
			pc.sendPackets(new S_OwnCharStatus(pc));
			pc.setAwakeSkillId(skillId);
			doPoly(pc,skillId);//TODO 3.52C龍騎士覺醒狀態外型調整變更
			pc.startMpReductionByAwake();
		}
	}

	public static void stop(final L1PcInstance pc) {
		final int skillId = pc.getAwakeSkillId();
		if (skillId == AWAKEN_ANTHARAS) {// 覚醒：アンタラス
			pc.addMaxHp(-35);
			pc.sendPackets(new S_HPUpdate(pc.getCurrentHp(), pc.getMaxHp()));
			if (pc.isInParty()) { // パーティー中
				pc.getParty().updateMiniHP(pc);
			}
			pc.addAc(8);
		} else if (skillId == AWAKEN_FAFURION) {// 覚醒：パプリオン
			pc.addMr(-25);
			pc.sendPackets(new S_SPMR(pc));
			pc.addWind(-15);
			pc.addWater(-15);
			pc.addFire(-15);
			pc.addEarth(-15);
			pc.sendPackets(new S_OwnCharAttrDef(pc));
		} else if (skillId == AWAKEN_VALAKAS) {// 覚醒：ヴァラカス
			pc.addStr(-3);
			pc.addCon(-3);
			pc.addDex(-3);
			pc.addCha(-3);
			pc.addInt(-3);
			pc.addWis(-3);
		}
		pc.sendPackets(new S_OwnCharStatus(pc));
		pc.setAwakeSkillId(0);
		undoPoly(pc);
		pc.stopMpReductionByAwake();
	}

	public static void undoPoly(final L1PcInstance pc) {
		final int classId = pc.getClassId();
		pc.setTempCharGfx(classId);
		pc.sendPackets(new S_ChangeShape(pc.getId(), classId));
		pc.broadcastPacketAll(new S_ChangeShape(pc.getId(), classId));
		final L1ItemInstance weapon = pc.getWeapon();
		if (weapon != null) {
			final S_CharVisualUpdate charVisual = new S_CharVisualUpdate(pc);
			pc.sendPackets(charVisual);
			pc.broadcastPacketAll(charVisual);
		}
	}

	private L1Awake() {
	}

}
